package com.zut.dao;

import com.zut.domain.Exam;
import com.zut.domain.ExamResult;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ExamResultDao {

    @Results(id = "groupWithExamResult",
            value = {
                    @Result(property = "userId", column = "user_id", id = true),
                    @Result(property = "examId", column = "exam_id", id = true),
                    @Result(property = "grade", column = "exam_grade"),
                    @Result(property = "date", column = "finish_date")
            })
    @Select("select * from exam_result")
    List<ExamResult> getAll();

    @Insert("insert into exam_result (user_id,exam_id,exam_grade,finish_date) " +
            "values(#{userId},#{examId},#{grade},#{date})")
    int insert(ExamResult examResult);

    @Delete("delete from exam_result where user_id = #{userId} and exam_id = #{examId}")
    int deleteById(String userId, String examId);

    @Update("<script>                                                           "
            + "update exam_result                                               "
            + "<set>                                                            "
            + "  <if test='grade != null'> exam_grade=#{grade},            </if>"
            + "  <if test='date != null'> finish_date=#{date},             </if>"
            + "</set>                                                           "
            + "where user_id = #{userId} and exam_id = #{examId}                "
            + "</script>                                                        ")
    int update(ExamResult examResult);

    @ResultMap("groupWithExamResult")
    @Select("select * from exam_result where user_id = #{id} order by finish_date desc")
    List<ExamResult> getById(String id);

    @Select("select user_id ,count(exam_grade is null) as status from exam_result group by user_id")
    List<Map<String,String>> getExamStatus();

    @ResultMap("groupWithExamResult")
    @Select("select * from exam_result where exam_result.exam_grade is null")
    List<ExamResult> getExamStatusIs();

    @Select("select count(exam_grade >= 80 or null)  from exam_result where user_id = #{id}")
    String getSubjectById(String id);

    @Select("select exam_id from exam_result where exam_grade is null and user_id = #{id}")
    String getExamId(String id);

    @Select("select exam_id from exam_result where user_id = #{id} and exam_grade is null")
    String getOrderExam(String id);

}
